package com.jn.fastjson.views;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jn.fastjson.R;

import java.util.ArrayList;
import java.util.List;

public class OlympicMedalActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_olympic_medal);

        String olympicJsonString = getIntent().getStringExtra("olympic_json");
        JSONObject olympicJson = JSON.parseObject(olympicJsonString);
        JSONArray medalsList = olympicJson.getJSONObject("result").getJSONArray("medalsList");

        List<String> medalItems = new ArrayList<>();
        for (int i = 0; i < medalsList.size(); i++) {
            JSONObject medal = medalsList.getJSONObject(i);
            String item = medal.getString("countryname") + " - 金牌: " + medal.getString("gold") +
                    ", 银牌: " + medal.getString("silver") + ", 铜牌: " + medal.getString("bronze");
            medalItems.add(item);
        }

        ListView medalListView = findViewById(R.id.medal_list);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, medalItems);
        medalListView.setAdapter(adapter);
    }
}
